﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKRoomInspection.FindRoomMethod;
using YJKRoomInspection.Model;
using YJKRoomInspection.Utiltiy;
using YArchitech.Revit;
using YJKRevitBase.WinForm.BackGroundProgressBar;
using YJKInspectModelFunc.ModelLayer;

namespace YJKRoomInspection.SpecialUse
{
	public class FrontRoomFind_CmdFrontRoomSurfaceCheck : IFrontRoomFind<ICloseCurveAnalays>, IPackerFind<ICloseCurveAnalays>, IBase<ICloseCurveAnalays>
	{
		public List<Element> m_FrontRoomElems
		{
			get
			{
				return this.frontRoomElems;
			}
			set
			{
				this.frontRoomElems = value;
			}
		}

		List<ICloseCurveAnalays> IPackerFind<ICloseCurveAnalays>.FindPackers(Autodesk.Revit.DB.Document inputDocumnt, params object[] others)
		{
			FrontRoomFind_CmdFrontRoomSurfaceCheck.m_doc = inputDocumnt;
			this.m_aryPhase = inputDocumnt.Phases;
			this.allLevel = (others[0] as List<Level>);
			ProgressBarManger progressBarManger = new ProgressBarManger(new BackWorkDelegate(this.FindRoomAreaCheck));
			progressBarManger.ShowFormAndDoWork(StaticRescource.m_hWndRevit);
			if (progressBarManger.IfManualAbsort)
			{
				return null;
			}
			return this.m_AllFrontRoom;
		}

		private void FindRoomAreaCheck()
		{
			this.m_FrontRoomElems = Utility.FilterElementFormDoc(StaticRescource.CmdData.Application.ActiveUIDocument, this.allLevel);
			new List<BaseRoomPacker>();
			List<BaseDoorPacker> roomPackers = new List<BaseDoorPacker>();
			List<BaseDoorPacker> stairRoomDoor = new List<BaseDoorPacker>();
			List<BaseDoorPacker> fireLadderDoor = new List<BaseDoorPacker>();
			for (int i = 0; i < this.m_aryPhase.Size; i++)
			{
                this.m_lstPhase.Add(this.m_aryPhase.get_Item(i));
			}
			this.m_AllFrontRoom = CloseCurveAnalyser.GetCloseCurveAnalysis(FrontRoomFind_CmdFrontRoomSurfaceCheck.m_doc, this.m_FrontRoomElems, this.allLevel, this.m_lstPhase, true);
			foreach (ICloseCurveAnalays closeCurveAnalays in this.m_AllFrontRoom)
			{
				roomPackers = Utility.FilterOutWall(closeCurveAnalays);
				Utility.FilterEauipmentDoor(closeCurveAnalays, roomPackers);
				stairRoomDoor = Utility.FindStairDoorNum(closeCurveAnalays, roomPackers);
				fireLadderDoor = Utility.FindXTDoor(closeCurveAnalays);
				closeCurveAnalays.AppendValue = this.ShortestSideIsRight(stairRoomDoor, fireLadderDoor);
			}
		}

		public double ShortestSideIsRight(List<BaseDoorPacker> StairRoomDoor, List<BaseDoorPacker> FireLadderDoor)
		{
			double result = 0.0;
			List<BaseDoorPacker> list = new List<BaseDoorPacker>();
			list.AddRange(StairRoomDoor);
			list.AddRange(FireLadderDoor);
			foreach (BaseDoorPacker baseDoorPacker in list)
			{
			}
			if (StairRoomDoor.Count == 0 || FireLadderDoor.Count == 0)
			{
				return result;
			}
			if (StairRoomDoor.Count == 2 || FireLadderDoor.Count == 2)
			{
				if (StairRoomDoor.Count == 2)
				{
					Wall wall = StairRoomDoor[0].ThisDoor.Host as Wall;
					Wall wall2 = StairRoomDoor[1].ThisDoor.Host as Wall;
					if (wall.Id != wall2.Id)
					{
						return result;
					}
					Element element = FireLadderDoor[0].ThisDoor.Host as Wall;
					Curve curve = (wall.Location as LocationCurve).Curve;
					Curve curve2 = (element.Location as LocationCurve).Curve;
                    if ((int)curve.Intersect(curve2) != 4)
					{
						return result;
					}
					result = new YJKLine(curve.GetEndPoint(0), curve.GetEndPoint(1)).GetDistance(curve2 as Line) * 0.3048;
				}
				else if (FireLadderDoor.Count == 2)
				{
					Wall wall = FireLadderDoor[0].ThisDoor.Host as Wall;
					Wall wall2 = FireLadderDoor[1].ThisDoor.Host as Wall;
					if (wall.Id != wall2.Id)
					{
						return result;
					}
					Element element2 = StairRoomDoor[0].ThisDoor.Host as Wall;
					Curve curve3 = (wall.Location as LocationCurve).Curve;
					Curve curve4 = (element2.Location as LocationCurve).Curve;
                    if ((int)curve3.Intersect(curve4) != 4)
					{
						return result;
					}
					result = new YJKLine(curve3.GetEndPoint(0), curve3.GetEndPoint(1)).GetDistance(curve4 as Line) * 0.3048;
				}
				return result;
			}
			return result;
		}

		private List<ICloseCurveAnalays> m_AllFrontRoom = new List<ICloseCurveAnalays>();

		private List<Element> frontRoomElems = new List<Element>();

		private List<Phase> m_lstPhase = new List<Phase>();

		private List<Level> allLevel = new List<Level>();

		public static Document m_doc;

		private PhaseArray m_aryPhase;
	}
}
